/*
 *@descrion:此封装方法应用于绝大多数请求
 */
import axios from "axios";
import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import router from "@/router";
import baseUrl from "@/api/baseURL";
const zhaxios = axios.create({
  baseURL: baseUrl.webServiceUrl,
  timeout: 40000, // 请求超时时间
});

// request interceptor
zhaxios.interceptors.request.use(
  (config) => {
    // 这个现在登录加上去就可以登录
    if (getToken("token")) {
      if(config.url.indexOf("/iot/") !== -1 || config.url.indexOf("base/files") !== -1||config.url.indexOf("/feedMachineConfig") !== -1 ||config.url.indexOf("/feedMachineWork") !== -1){
        config.baseURL = 'http://119.23.242.86:9090'
      }
      
      config.headers["token"] = getToken("token");
      // 过期的token 用于测试token过期
      // config.headers["token"] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJob21lQmFzZVR5cGUiOjEsImV4cCI6MTY4MDEzOTI4NSwidXNlcklkIjo1LCJzdGFmZklkIjo4fQ.vB_w6-VbFBYvhBw1_QobtKDVvoGT2W2e1W_TK-bIAKM';
      config.headers["homeBaseId"] = sessionStorage.getItem("homeBaseId");
    } else {
      if (config.url.indexOf("/user/userLogin") == -1) {
        Message({
          message: "身份信息已过期,请重新登陆",
          type: "warning",
          duration: 3 * 1000,
        });
        router.push({ path: "/login" });
      }
    }
    if (config.url.includes("/add")) {
      config.data.homeBaseId = sessionStorage.getItem("homeBaseId");
    }
    if (config.url.includes("/nc/basicInfo/")) {
      config.data = { ...config.data, pk_org: JSON.parse(localStorage.ncOrg) };
    }
    if (config.url.includes("ncDeadRecord/add")) {
      delete config.data.homeBaseId;
    }
    console.log(config);
    return config;
  },
  (error) => {
    // console.log(error + 'yyyyyyyyyyy');
    return Promise.reject(error);
  }
);

// response interceptor
zhaxios.interceptors.response.use(
  (response) => {
    if (response.success === false && errCode === 200) {
      Message({
        message: response.message,
        type: "error",
        duration: 3 * 1000,
      });
    }
    if (
      response.request.responseURL.includes("/export") ||
      response.request.responseURL.includes("/download") ||
      response.request.responseURL.includes("/exprotHistory") ||
      response.request.responseURL.includes("/reportWaterQuality") ||
      response.request.responseURL.includes("/downloadTemplate") ||
      response.request.responseURL.includes("/exportQtjl") ||
      response.request.responseURL.includes("/materialReport") ||
      response.request.responseURL.includes("/finprodinReportReport") ||
      response.request.responseURL.includes("/generaloutReportReport") ||
      response.request.responseURL.includes("/animalProtectionInventoryReport")
    ) {
      return response;
    }
    const res = response.data;
    return res;
  },
  (error) => {
    console.log(error.response.status);
    if (error.response.status === 401) {
      // token过期处理
      Message({
        message: "身份信息已过期,请重新登陆",
        type: "warning",
        duration: 3 * 1000,
      });
      sessionStorage.removeItem("ss_userInfo");
      sessionStorage.clear();
      router.push({ path: "/login" });
    }
    console.log("err" + error);
    return Promise.reject(error);
  }
);
export default {
  get(url, params) {
    var str = "?";
    for (let key in params) {
      var s = "";
      s = key + "=" + params[key];
      if (str === "?") str += s;
      else str += "&" + s;
    }
    var ur1 = url + (str === "?" ? "" : str);
    return zhaxios({
      method: "get",
      url: ur1,
    });
  },
  fileGet(url, params) {
    var str = "?";
    for (let key in params) {
      var s = "";
      s = key + "=" + params[key];
      if (str === "?") str += s;
      else str += "&" + s;
    }
    var ur1 = url + (str === "?" ? "" : str);
    return zhaxios({
      method: "get",
      url: ur1,
      responseType: "blob",
      params,
    });
  },
  post(url, data) {
    return zhaxios({
      method: "post",
      url,
      data,
    });
  },
  filePost(url, data) {
    return zhaxios({
      method: "post",
      url,
      responseType: "blob",
      data,
    });
  },
  formDataPost(url, data) {
    return zhaxios({
      method: "post",
      url,
      headers: {
        "Content-Type": "multipart/form-data",
      },
      data,
    });
  },
};
